﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace HauhaltsPlaner
{
    class IOHandler
    {
        public void saveDataTable(string profil="",string typ="", DataTable dt=null)
        {
            //Profildaten in ./ path 
            //alles andere in ./profilname/path
            if (profil == "")
            {
                
                dt.TableName = "ProfilData";
                dt.WriteXml(@".\ProfilData.xml");
            }
            else
            {
                string path=(".//"+profil+"//");
                dt.TableName = typ;
                if (Directory.Exists(path))
                {
                    dt.WriteXml(path + typ + ".xml");
                }
                else
                {
                    Directory.CreateDirectory(path);
                    dt.WriteXml(path + typ + ".xml");
                }
            }

        }

        public DataTable loadDataTable(string profil = "", string typ ="")
        {
            //Profildaten in ./ path 
            //alles andere in ./profilname/path
            string path = "./" + profil + "/" + typ + ".xml";
            string directory = "./" + profil + "/";
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            if (profil == "")
            {

                if (File.Exists("./ProfilData.xml"))
                {
                    ds.ReadXml("./ProfilData.xml");
                }
            }
            else
            {
               
                if (Directory.Exists(directory))
                {
                    if (File.Exists(path))
                    {
                        ds.ReadXml(path);
                    }
                }
            }
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }
            else
            {
                dt = null;
            }
            return dt;
        }

        public void deleteProfile(string profil = "")
        {
            File.Delete("./" + profil);
        }


    }
}
